﻿Imports System.Data.SqlClient

Public Class clsLicencia
    Private mintidEmpleado As Integer
    Private mstrLicencia As String
    Private mstrtipo As String
    Private mdatVigencia As Date

    Public Property idempleado() As Integer
        Get
            Return mintidEmpleado
        End Get
        Set(ByVal value As Integer)
            mintidEmpleado = value
        End Set
    End Property
    Public Property Vigencia() As Date
        Get
            Return mdatvigencia
        End Get
        Set(ByVal value As Date)
            mdatvigencia = value
        End Set
    End Property

    Public Property Licencia() As String
        Get
            Return mstrLicencia
        End Get
        Set(ByVal value As String)
            mstrLicencia = value
        End Set
    End Property
    Public Property Tipo() As String
        Get
            Return mstrtipo
        End Get
        Set(ByVal value As String)
            mstrtipo = value
        End Set
    End Property
    Public Sub New()
        mintidEmpleado = 0
        mdatVigencia = Now()
        mstrtipo = ""
        mstrLicencia = ""
    End Sub
    Public Sub Guardar()

        If idempleado = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer

        intRetVal = BuscarIntDatoCitra("Licencias", "idempleado", "idempleado=" & idempleado)


        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()

        End If

    End Sub
    Public Sub Borrar()
        Call SetSqlCitra("Delete From Licencias Where idempleado = " & idempleado)

    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Licencias " & _
        " WHERE idempleado=" & idempleado

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mintidEmpleado = rdBuscar("idempleado")
            mdatVigencia = rdBuscar("vigencia")
            mstrtipo = rdBuscar("tipo").ToString.Trim
            mstrLicencia = rdBuscar("licencia").ToString.Trim
        Else
            mintidEmpleado = 0
            mdatVigencia = Now()
            mstrtipo = ""
            mstrLicencia = ""
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Licencias (" & _
                            "IdEmpleado," & _
                            "Licencia," & _
                            "Tipo," & _
                            "Vigencia)" & _
                            "VALUES (" & _
                            "@IdEmpleado," & _
                            "@Licencia," & _
                            "@Tipo," & _
                            "@Vigencia)"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdEmpleado", mintidEmpleado)
        Command.Parameters.AddWithValue("Licencia", mstrLicencia)
        Command.Parameters.AddWithValue("Tipo", mstrtipo)
        Command.Parameters.AddWithValue("Vigencia", mdatVigencia)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
      Public Sub Actualizar()
        Dim sql As String = "UPDATE Licencias SET " & _
                            "Licencia=@Licencia," & _
                            "Vigencia=@Vigencia " & _
                            "WHERE IdEmpleado=@IdEmpleado"



        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdEmpleado", mintidEmpleado)
        Command.Parameters.AddWithValue("Licencia", mstrLicencia)
        Command.Parameters.AddWithValue("Tipo", mstrtipo)
        Command.Parameters.AddWithValue("Vigencia", mdatVigencia)


        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If

    End Sub
End Class
